package com.zego.wawaji.ui.Fragment.play;

import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.zego.wawaji.R;
import com.zego.wawaji.base.module.ModuleMgr;
import com.noble.library.observer.MsgMgr;
import com.zego.wawaji.base.observer.MsgType;
import com.noble.library.observer.PObserver;
import com.noble.library.basic.string.StringUtils;
import com.zego.wawaji.http.request.HttpResponse;
import com.zego.wawaji.http.request.RequestComplete;
import com.zego.wawaji.http.request.UrlParam;
import com.zego.wawaji.third.share.ShareUtils;
import com.zego.wawaji.third.share.util.ShareEnum;
import com.zego.wawaji.ui.dialog.popup.BottomPopup;

import org.json.JSONObject;

/**
 * 每日礼包弹框
 * Created by Su on 2018/3/31.
 */
public class EveryGiftBagDialog extends BottomPopup implements RequestComplete, PObserver {
    private TextView tv_title, tv_remark, btn_submit;
    private boolean hasGet = false;  // 是否已经领取

    public EveryGiftBagDialog(FragmentActivity activity) {
        super(activity);
        MsgMgr.getInstance().attach(this);
        ShareUtils.getInstance().reqShareData(null);
    }

    @Override
    protected View makeContentView() {
        View inflate = LayoutInflater.from(activity).inflate(R.layout.every_gift_dialog, null);
        tv_title = (TextView) inflate.findViewById(R.id.tv_title);
        tv_remark = (TextView) inflate.findViewById(R.id.tv_remark);
        btn_submit = (TextView) inflate.findViewById(R.id.btn_submit);
        tv_remark.setText(StringUtils.replaceTag(ModuleMgr.getWawaMgr().getCommon().getRewardDailyContent(), "|", "\n"));

        btn_submit.setOnClickListener(v -> {
            if (!hasGet) {
                ModuleMgr.getWawaMgr().reqRewardDaily(EveryGiftBagDialog.this);
            } else {
                ShareUtils.shareChannel = ShareUtils.SHARE_REWARD;
                //PickerDialogUtil.showShareDialog(getActivity());
                ShareUtils.getInstance().shareData(getActivity(), ShareEnum.WEIXIN);
            }
        });
        inflate.findViewById(R.id.btn_close).setOnClickListener(v -> close());
        return inflate;
    }

    @Override
    public void onRequestComplete(HttpResponse response) {
        if (response.getUrlParam() == UrlParam.reqRewardDaily) {
            if (!response.isOk()) {
                close();
                return;
            }

            JSONObject jsonObject = response.getResponseObject();
            String title = jsonObject.optString("Title");
            String content = jsonObject.optString("Content");
            tv_title.setText(title);
            tv_remark.setText(StringUtils.replaceTag(content, "|", "\n"));
            btn_submit.setText("分享领奖励");
            hasGet = true;
        }
    }

    private void close() {
        MsgMgr.getInstance().detach(this);
        dismiss();
    }

    @Override
    public void onMessage(String key, Object value) {
        switch (key) {
            case MsgType.MT_Reward_Daily:
                close();
                break;
        }
    }
}